package org.hb0712.sya.tools.database.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hb0712.sya.tools.database.dao.TableDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TableService {
    @Autowired
    public TableDao tableDao;
    @Autowired
    private Map<String, String[]> tablenames;
    @Autowired
    private Map<String, String[]> tablenames3;
    @Autowired
    private Map<String, String[]> tablenames4;
    @Autowired
    private Map<String, String[]> tablenames6;
    
    public List<Map<String, Object>> structure(String tablename) {
        if (tablename == null || tablename.isEmpty()) {
            return null;
        }
        
        var list = tableDao.getStructureDev(tablename);
        var list2 = tableDao.getStructureProd(tablename);
        
        for (Map<String, Object> item : list) {
            if (item.get("DATA_TYPE")!=null && "NUMBER".equals(item.get("DATA_TYPE").toString())) {
                if (item.get("DATA_PRECISION")!=null && item.get("DATA_SCALE")!=null) {
                    String data_type = item.get("DATA_TYPE") + "(" + item.get("DATA_PRECISION") + "," + item.get("DATA_SCALE") + ")";
                    item.put("DATA_TYPE", data_type);
                } else if (item.get("DATA_SCALE")!=null) {
                    String data_type = item.get("DATA_TYPE") + "(38," + item.get("DATA_SCALE") + ")";
                    item.put("DATA_TYPE", data_type);
                }
            }
            String column_name = item.get("COLUMN_NAME").toString();
            item.put("COLUMN_NAME2", null);
            item.put("DATA_TYPE2", null);
            for (Map<String, Object> item2 : list2) {
                String column_name2 = item2.get("COLUMN_NAME").toString();
                if (column_name2.equals(column_name)) {
                    item.put("COLUMN_NAME2", item2.get("COLUMN_NAME"));
                    item.put("DATA_PRECISION2", item2.get("DATA_PRECISION"));
                    item.put("DATA_SCALE2", item2.get("DATA_SCALE"));
                    if (item2.get("DATA_TYPE")!=null && "NUMBER".equals(item2.get("DATA_TYPE").toString())) {
                        String data_type = item2.get("DATA_TYPE").toString();
                        if (item2.get("DATA_PRECISION")!=null && item2.get("DATA_SCALE")!=null) {
                            data_type += "(" + item2.get("DATA_PRECISION") + "," + item2.get("DATA_SCALE") + ")";
                        } else if (item2.get("DATA_SCALE")!=null) {
                            data_type += "(38," + item2.get("DATA_SCALE") + ")";
                        }
                        item.put("DATA_TYPE2", data_type);
                    } else {
                        item.put("DATA_TYPE2", item2.get("DATA_TYPE"));
                    }
                    break;
                }
            }
        }
        
        return list;
    }
    
    public List<Map<String, Object>> data(String tablename, String TABLE_NAME) {
        if (tablename == null || tablename.isEmpty()) {
            return null;
        }
        
        var list = tableDao.getDataDev(tablename, TABLE_NAME);
        var list2 = tableDao.getDataProd(tablename, TABLE_NAME);
        
        for (Map<String, Object> item:list) {
            var keys = item.keySet().toArray();
            for (int i=0;i<keys.length;i++) {
                String key = keys[i].toString();
                item.put(key+"2", null);
            }
            item.put("same", "");
            item.put("ID2", "");
        }
        
        for (Map<String, Object> item:list) {
            String column_name = item.get("NAME").toString();
            for (Map<String, Object> item2:list2) {
                String column_name2 = item2.get("NAME").toString();
                if (column_name2.equals(column_name)) {
                    String same = "";
                    for (String key:item2.keySet()) {
                        item.put(key+"2", item2.get(key));
                        
                        if (item2.get(key)!=null && item.get(key)!=null && !item2.get(key).equals(item.get(key))) {
                            same = "false";
                        }
                    }
                    item.put("same", same);
                }
            }
        }
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> m1, Map<String, Object> m2) {
                return ((String) m1.get("NAME")).compareTo((String) m2.get("NAME"));
            }
        });
        //return tableDao.getData(tablename);
        return list;
    }
    
    public String[] getTitle(String tablename) {
        String[] title = tablenames4.get(tablename);
        return title;
    }
    
    public String[] getNameEN(String tablename) {
        String[] nameEN = tablenames3.get(tablename);
        return nameEN;
    }
    
    public Map<String, List<Map<String, String>>> getTableNames() {
        var result = new HashMap<String, List<Map<String, String>>>();
        for (String key:tablenames.keySet()) {
            var list = new ArrayList<Map<String, String>>();
            var names = tablenames.get(key);
            for (String name: names) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("name", name);
                map.put("error", "");
                var structure = structure(name);
                for (var item : structure) {
                    if (item.get("DATA_TYPE")!=null && item.get("DATA_TYPE2")!=null
                            && !item.get("DATA_TYPE").toString().equals(item.get("DATA_TYPE2").toString())) {
                        map.put("error", " (*)");
                        break;
                    }
                    if (item.get("DATA_TYPE")!=null && item.get("DATA_TYPE2")==null) {
                        map.put("error", " (-)");
                    }
                }
                list.add(map);
            }
            result.put(key, list);
        }
        return result;
    }
    
    public Map<String, List<Map<String, String>>> getTableNames6() {
        var result = new HashMap<String, List<Map<String, String>>>();
        for (String key:tablenames6.keySet()) {
            var list = new ArrayList<Map<String, String>>();
            var names = tablenames.get(key);
            int max = tablenames3.get("ZZYY_T_XH_S_FIELD_CONFIG").length;
            for (String name: names) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("name", name);
                map.put("error", "");
                var data = data("ZZYY_T_XH_S_FIELD_CONFIG", name);
                for (var item : data) {
                    var keys = item.keySet().toArray();
                    if (item.get("DATA_TYPE")!=null && item.get("DATA_TYPE2")!=null
                            && !item.get("DATA_TYPE").toString().equals(item.get("DATA_TYPE2").toString())) {
                        map.put("error", " (*)");
                        break;
                    }
                    if (item.get("DATA_TYPE")!=null && item.get("DATA_TYPE2")==null) {
                        map.put("error", " (-)");
                    }
                }
                list.add(map);
            }
            result.put(key, list);
        }
        return result;
    }
    
    public boolean reload() {
        tableDao.setStructureDev();
        tableDao.setStructureProd();
        return true;
    }
}
